<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-11-20 16:15
 *
 * Version: 2.0
 */

namespace App\Modules\Order\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class GetOrderDetailInfoIncludeItemsByOrderIdCriteria extends BaseCriterias
{
	protected $store_id;
	protected $is_barber;
	protected $user_id;
	protected $status;
	protected $source;
	protected $keywords;
	protected $is_pad;
	protected $orderIds;

	public function __construct($store_id=null, $is_barber=null, $user_id=null, $status=null, $source=null, $keywords=null, $orderIds = [])
	{
		$this->store_id = $store_id;
		$this->is_barber = $is_barber;
		$this->user_id = $user_id;
		$this->status = $status;
		$this->source = $source;
		$this->keywords = $keywords;
		$this->orderIds = $orderIds;
	}


	public function apply($model, RepositoryInterface $repository)
	{
		$store_id = $this->store_id;
		$is_barber = $this->is_barber;
		$user_id = $this->user_id;
		$status = $this->status;
		$source = $this->source;
		$keywords = $this->keywords;
		$orderIds = $this->orderIds;

		return $model
			->with('items')
			->when(!empty($orderIds), function ($query) use ($orderIds) {
				if(is_array($orderIds)){
					$query->whereIn('id', $orderIds);
				}else{
					$query->where('id',$orderIds);
				}
			})
			->when(!empty($store_id),function ($query) use($store_id){
				$query->where('store_id', $store_id);
			})
			->when($is_barber, function ($query) use ($user_id) {
				$query->where('barber_id', $user_id);
			})
			->when(isset($status), function ($query) use ($status) {
				$query->where('status', $status);
			})
			->when(!empty($source), function ($query) use ($source) {
				$query->where('source', $source);
			})
			->when(!empty($keywords), function ($query) use ($keywords) {
				$query->where('nick', 'like', '%' . $keywords . '%')
					->orWhere('mobile', 'like', '%' . $keywords . '%');
			})
			->orderBy('created_at','desc');

	}
}